package sweet.toolbox

/** Main entry point of Sweet Toolbox. */
object Main extends sweet.toolbox.command.ToolboxConfig {
  import sweet.toolbox.command._
  
  // Register all avialable command.
  addCommands(
    "list" -> new ListSummary(commands),
    "create-project" -> new CreateProject(props),
    "create-controller" -> new CreateController(props)
  )
  
  // Main usage info.
  val usage = """
    | Sweet Commands Toolbox. 
    |
    | To use it, invoke with one of avialable commands:
    |   toolbox <commandName> [commandArguments ...]
    | 
    | To see a list of available commads:
    |   toolbox list
    |
    | To see individual command help:
    |   toolbox <commandName> help
  """.stripMargin
  
  def main(args: Array[String]){
    if(args.length < 1)
      throw new IllegalArgumentException(usage)
      
    val cmdName = args(0)
    val cmdArgs = args.drop(1)
    
    if(cmdName == "help" || cmdName == "--help" || cmdName == "-h")
      println(usage)
    else
      commands.get(cmdName) match {
        case Some(cmd) => {
          if(cmdArgs.length == 1 && cmdArgs(0) == "help")
            println(cmd.usage)
          else
            cmd.exec(cmdArgs)
        }
        case None => throw new IllegalArgumentException("Command "+cmdName+" not found")
      }
  }
}

